package com.thread.example.controller;

import com.thread.example.service.AsyncDemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.*;

/**
 * @Auther:
 * @Description: //注意：1、要把异步任务写进一个类里面而不能直接写在controller中
 * //      2、如果需要拿到结果，则需要在调用处判断全部的task.isDone()来确定异步任务的完成
 * @Date: 2019/11/1 16:32
 */
@RestController
@RequestMapping("/async")
@Api(value = "asyncController", description = "asyncController")
public class AsyncController {

    @Autowired
    private AsyncDemoService asyncDemoService;

    @PostMapping("/a")
    @ApiOperation(value = "/a", notes = "/a")
    public void a() throws Exception {
        long l = System.currentTimeMillis();
        List<String> list = new LinkedList<>();
        List<Future<String>> futures = new LinkedList<>();
        CountDownLatch latch = new CountDownLatch(100);
        for (int i = 0; i < 100; i++) {
            Future<String> future = asyncDemoService.asyncService(list, i, latch);
            futures.add(future);
        }
        latch.await();
        for (Future<String> future : futures) {
            String s = future.get();
            list.add(s);
        }
        System.out.println(" a   sout list");
        list.forEach(s -> System.out.println("a==" + s));
        long l2 = System.currentTimeMillis();
        System.out.println("总耗时：" + (l2 - l));
    }

    @PostMapping("/a1")
    @ApiOperation(value = "/a1", notes = "/a1")
    public void a1() throws Exception {
        long l = System.currentTimeMillis();
        List<Future<String>> futures = new LinkedList<>();
        List<String> list = new LinkedList<>();
        for (int i = 0; i < 100; i++) {
            Future<String> future = asyncDemoService.asyncService1(list, i);
            futures.add(future);
        }
        for (Future<String> future : futures) {
            String s = future.get();
            list.add(s);
        }
        System.out.println("sout list");
        list.forEach(s -> System.out.println("a1==" + s));
        long l2 = System.currentTimeMillis();
        System.out.println("a1 总耗时：" + (l2 - l));
    }


    @PostMapping("/zuse")
    @ApiOperation(value = "/zuse", notes = "/zuse")
    public void zuse() throws Exception {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            list.add(i);
        }
//        List<FutureTask<Integer>> zuse = asyncDemoService.zuse(list);
        Queue<FutureTask<Integer>> zuse = asyncDemoService.zuse(list);
        while (true) {
            FutureTask<Integer> futureTask = zuse.poll();
            if (futureTask == null)
                break;
            try {
                Integer s = futureTask.get(10l, TimeUnit.SECONDS);
                System.out.println(s);
            } catch (TimeoutException ex) {
                System.out.println("---");
                zuse.add(futureTask);
            }
        }
//        for(FutureTask<Integer> futureTask : zuse){
//            //try{
//                Integer s = futureTask.get();//3l, TimeUnit.SECONDS);
//                System.out.println(s);
////            }catch (TimeoutException ex){
////                System.out.println("---");
////            }
//        };
        System.out.println("方法完成");
    }
}
